package at.fhj.itm10.ticketmaster.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

import org.apache.log4j.Logger;

import at.fhj.itm10.ticketmaster.entities.Ticket;

/**
 * @author 	Kevin Reinmueller
 * @since	November 2012
 */

public class TicketDAO extends AbstractDAO {	

	//	--
	//	-- Tabellenstruktur für Tabelle `ticket`
	//	--
	//
	//	CREATE TABLE IF NOT EXISTS `ticket` (
	//	  `id` int(11) NOT NULL AUTO_INCREMENT,
	//	  `fk_order_id` int(11) NOT NULL,
	//	  `fk_events_id` int(11) NOT NULL,
	//	  `quantity` int(11) NOT NULL COMMENT 'Max. 6!!!',
	//	  `add_to_cart_date` datetime NOT NULL,
	//	  PRIMARY KEY (`id`)
	//	) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
	
	private final static String PRIMARYKEY_NAME = "id";
	private final static String SQL_INSERT ="INSERT INTO `ticket` (`fk_order_id`, `fk_events_id`, `quantity`, `add_to_cart_date`) VALUES (?,?,?,?)";
	private final static String SQL_UPDATE = "UPDATE `ticket` SET `fk_order_id` = ?, `fk_events_id` = ?, `quantity` = ?, `add_to_cart_date` = ? WHERE `id` = ?";
	private final static String SQL_DELETE = "DELETE FROM `ticket` WHERE `id` = ?";
	private final static String SQL_FINDBYID = "SELECT * FROM `ticket` WHERE `id` = ?";
	private final static String SQL_FINDALL = "SELECT * FROM `ticket`";
	private Logger logger = Logger.getLogger(TicketDAO.class);

	public TicketDAO(Connection connection) throws DAOException {
		super(connection);
	}
	
	public void insert(Ticket entity) throws DAOException {
		logger.debug("TicketDAO.insert(Ticket = " + entity.toString() + ")");
		checkForValidEntity(entity);
		PreparedStatement statement = prepareStatement(SQL_INSERT);

		try {
			statement.setLong(1, entity.getFk_order_id());
			statement.setLong(2, entity.getFk_events_id());
			statement.setInt(3, entity.getQuantity());
			statement.setString(4, convertDateToString(entity.getAdd_to_cart_date()));
		} catch (Exception e) {
			throw new DAOException(e);
		}

		super.processInsert(statement, entity);
	}

	public void update(Ticket entity) throws DAOException {
		logger.debug("TicketDAO.update(Ticket = " + entity.toString() + ")");
		checkForValidEntity(entity);
		PreparedStatement statement = prepareStatement(SQL_UPDATE);
		
		try {

			statement.setLong(1, entity.getFk_order_id());
			statement.setLong(2, entity.getFk_events_id());
			statement.setInt(3, entity.getQuantity());
			statement.setString(4, convertDateToString(entity.getAdd_to_cart_date()));
			statement.setLong(5, entity.getId());
		} catch (Exception e) {
			throw new DAOException(e);
		}

		super.processUpdate(statement, entity);
	}

	public void delete(Ticket entity) throws DAOException {
		logger.debug("TicketDAO.delete(Ticket = " + entity.toString() + ")");
		checkForValidEntity(entity);

		// Entity gets deleted by its id automatically, you just need
		// one placeholder in your DELETE query
		super.doDelete(prepareStatement(SQL_DELETE), entity);
	}

	public Ticket findById(long id) throws DAOException {
		logger.debug("TicketDAO.findById(Ticket = " + id + ")");
		Ticket entity = (Ticket) retrieveEntity(id);
		if (entity != null) {
			return entity;
		}

		// Do not set anything for the find-one query, as this is
		// done in super.doFindOne()
		return (Ticket) super.doFindOne(id, prepareStatement(SQL_FINDBYID));
	}

	public List<Ticket> findAll() throws DAOException {
		logger.debug("TicketDAO.findAll()");
		return super.doFindMore(PRIMARYKEY_NAME, prepareStatement(SQL_FINDALL));
	}

	/**
	 * {@inheritDoc}
	 */
	protected Ticket fillEntityByResult(ResultSet resultSet) throws DAOException {
		logger.debug("TicketDAO.fillENtityByResult(Ticket ResultSet= " + resultSet.toString() + ")");
		Ticket order = new Ticket();
		
		try {
			order.setId(resultSet.getLong(PRIMARYKEY_NAME));
			order.setFk_events_id(resultSet.getLong("fk_events_id"));
			order.setFk_order_id(resultSet.getLong("fk_order_id"));
			order.setQuantity(resultSet.getInt("quantity"));
			order.setAdd_to_cart_date(convertStringToDate(resultSet.getString("add_to_cart_date")));
		} catch (Exception e) {
			throw new DAOException(e);
		}

		return order;
	}
}

